unit modTunnelSessionId;

interface

uses Classes,
     ESBDates,
     SysUtils,
     sFonctionsChaines;


const
   TUNNEL_OPCODE_SPIDER_DO_LOGIN = 1;
   TUNNEL_OPCODE_STATS_RECOLTE   = 2;
   TUNNEL_OPCODE_LIRE_MSG        = 3;
   TUNNEL_OPCODE_SPIDER_SCAN     = 4;



  // données chiffrées avec la clé privée
  TUNNEL_FORUM_DATA =  '';


(*
http://127.0.0.1:64508/ru/
*)
  TUNNEL_IDENT_FORTE  = '';


    TUNNEL_COOKIE_AUTH_SESSION_ID = 'tid';
    TUNNEL_POST_NAME_USERNAME   = 'user';
    TUNNEL_POST_NAME_PASSWORD   = 'pass';




    TUNNEL_ROBOT_POST_OPERATION = 'action';

    TUNNEL_ROBOT_POSTVAL_OPERATION_PROFIL_LOAD = 'profil_load';
    TUNNEL_ROBOT_POSTVAL_PROFIL_LOAD_PROFILID = 'profil';

    TUNNEL_ROBOT_POSTVAL_OPERATION_GALAXY_LOAD = 'galaxy_load';
    TUNNEL_ROBOT_POSTVAL_GALAXY_LOAD_ID        = 'galaxy';


    
    TUNNEL_ROBOT_POSTVAL_OPERATION_STATS_RECOLTE = 'stats_recolte';

    TUNNEL_ROBOT_POSTVAL_OPERATION_SPIDER_LOGIN  = 'spider_login';
    TUNNEL_ROBOT_POSTVAL_OPERATION_LIRE_MSG      = 'lire_msg';

    TUNNEL_ROBOT_POSTVAL_OPERATION_SPIDER_SCAN         = 'spider_scan';


    TUNNEL_ROBOT_POSTVAL_OPERATION_ATTACK    = 'attack';
    TUNNEL_ROBOT_POSTVAL_ATTACK_CIBLE        = 'target';

    TUNNEL_ROBOT_POSTVAL_OPERATION_SONDAGE_AJOUT_INACTIF     = 'ajouterinactif';
    TUNNEL_ROBOT_POSTVAL_OPERATION_SONDAGE_AJOUT_PAR_POINTS  = 'ajouterparpoints';
    TUNNEL_ROBOT_POSTVAL_OPERATION_SONDAGE_SET_OPTIONS       = 'sondage_set_options';

    TUNNEL_ROBOT_POSTVAL_OPERATION_SONDAGE_CLEAR  = 'sondage_clear';
    TUNNEL_ROBOT_POSTVAL_OPERATION_SONDAGE_START  = 'sondage_start';
    TUNNEL_ROBOT_POSTVAL_OPERATION_SONDAGE_STOP   = 'sondage_stop';


    //////////////////////////////////////////////////////////////
    // Dossiers et fichiers
    //////////////////////////////////////////////////////////////

    TUNNEL_RELAY_DIR = '/relay/';
    TUNNEL_RELAY_FILE_LOGIN_PAGE    = TUNNEL_RELAY_DIR + 'login.htm';
    TUNNEL_RELAY_FILE_LOGIN_ECHEC   = TUNNEL_RELAY_DIR + 'login_echec.htm';
    TUNNEL_RELAY_FILE_LOGIN_SUCCESS = TUNNEL_RELAY_DIR + 'login_success.htm';

    TUNNEL_RELAY_FILE_INVALID       = TUNNEL_RELAY_DIR + 'login_success.htm';
    TUNNEL_RELAY_FILE_SELECTION_SERVICE       = TUNNEL_RELAY_DIR + 'selection_service.htm';

    TUNNEL_GAME_DIR = '/game/';
    TUNNEL_GAME_FILE_FRAMESET       = TUNNEL_GAME_DIR + 'frameset.html';
    TUNNEL_GAME_FILE_NOT_LOGGED     = TUNNEL_GAME_DIR + 'not_logged.html';
    TUNNEL_GAME_FILE_PAGE_REFUSEE   = TUNNEL_GAME_DIR + 'page_refusee.html';

    TUNNEL_GAME_REQFILE_LEFT_MENU      = 'leftmenu.html';
    TUNNEL_GAME_REQFILE_VUE_GENERALE   = 'overview.html';

    TUNNEL_GAME_FILE_NON_LOGGED     = TUNNEL_GAME_DIR + 'not_logged.html';

    TUNNEL_GAME_SESSION_ID = 'xtid';

    TUNNEL_GAME_DIR_MISC             = TUNNEL_GAME_DIR + 'misc/';
    TUNNEL_GAMEMISC_APERCU           = TUNNEL_GAME_DIR_MISC + 'apercu.html';



    TUNNEL_ROBOT_DIR = '/robot/';
    TUNNEL_ROBOT_FILE_PROFIL_SELECTION = TUNNEL_ROBOT_DIR + 'profil_selection.htm';
    TUNNEL_ROBOT_FILE_PROFIL_VOIR = TUNNEL_ROBOT_DIR + 'profil_selection.htm';
    TUNNEL_ROBOT_FILE_INDEX = TUNNEL_ROBOT_DIR + 'index.htm';
    TUNNEL_ROBOT_FILE_PROFIL_STATUS = TUNNEL_ROBOT_DIR + 'status.htm';
    TUNNEL_ROBOT_FILE_PROFIL_STATUS_CLEAR = TUNNEL_ROBOT_DIR + 'status_clear.htm';
    TUNNEL_ROBOT_FILE_PROFIL_SONDAGE_STOP = TUNNEL_ROBOT_DIR + 'on_sondages_stop.htm';

    TUNNEL_ROBOT_FILE_PROFIL_SONDAGE_VIEW = TUNNEL_ROBOT_DIR + 'sondage_view.htm';

    TUNNEL_ROBOT_FILE_PROFIL_RAPPORT_RECENT_VIEW = TUNNEL_ROBOT_DIR + 'recent_view.htm';





    TUNNEL_FORUM_DIR = '/forum/';
    TUNNEL_FORUM_FILE_ACCESS_REFUSE = TUNNEL_FORUM_DIR + 'access_refuse.htm';
    TUNNEL_FORUM_FILE_NOT_CONFIG    = TUNNEL_FORUM_DIR + 'userpass_pas_configure.htm';
    TUNNEL_FORUM_FILE_NOFORUMFOUND    = TUNNEL_FORUM_DIR + 'forum_notfound.htm';



type
    TForumDataConn = record
       URL         : string;
       URLForModif  : string;
       URLDirOnly  : string;

       DataForLogin: string;
    end;

    
    TTunnelSessionId = record
        // cookies:
        sessionid: string;

        // nom d'utilisateur
        Utilisateur: string;

        ip: string;

        // niveau d'accï¿½s
        Access: integer;

        // expiration
        DateExp: int64;
    end;


    PTunnelSessionId = ^TTunnelSessionId;
    TCollectionTunnelSessionId = class(TList)
    private
       {--------------------------------------------------}
       {Elements spï¿½cialisï¿½s de la classes}


       {--------------------------------------------------}
       {Gestion des pointeurs}
       // Utilisï¿½ pour la propriï¿½tï¿½ Itemx
       function GetItemx(Index: integer): TTunnelSessionId;
    public

       {--------------------------------------------------}
       {Gestion des pointeurs}
       // *** Ajout de pointeurs
       // Renvoi le pointeur de l'ï¿½lï¿½ment ajoutï¿½
       function Add(Element: TTunnelSessionId): PTunnelSessionId;
      
       // Simple ajout de pointeur
       procedure SimpleAdd(Pointeur: PTunnelSessionId);
      
       // Renvoi le pointeur de l'ï¿½lï¿½ment ajoutï¿½/modifiï¿½
       // Utilise Get_Index pour vï¿½rifier si l'ï¿½lï¿½ment doit ï¿½tre mis ï¿½ jour
       function Add_Update(Element: TTunnelSessionId): PTunnelSessionId;
      
      
       // *** Suppression de pointeur
       // Libï¿½re et supprime le pointeur Index
       // Renvoi True si rï¿½ussit, False si hors limite ou erreur
       function Delete(Index: integer): boolean; overload;

       // Cherche Element avec Get_Index et supprime LE
       // PREMIER pointeur trouvï¿½, renvoi True si rï¿½ussit
       function Delete(Element: string): boolean; overload;

      
       // *** Effacement de la liste de pointeur
       procedure Clear; override;
      
      
       // *** Requete sur la liste de pointeur
       function Get_Index(const Element: string): integer;
       function Get_Pointeur(const Element: string): PTunnelSessionId;
       function Get_Item(Index: integer): TTunnelSessionId;
       property Itemx[Index: integer]: TTunnelSessionId read GetItemx;
      

      
       {--------------------------------------------------}
       {Procï¿½dures et fonctions spï¿½cialisï¿½es de la classes}
       // Destructeur
       destructor Destroy; override;
       constructor Create;


    end;


    procedure tsiExtractForumURL(var Infos: TForumDataConn);
    

implementation

uses modInteractionAuteur, modInteractionKey;

procedure tsiExtractForumURL(var Infos: TForumDataConn);
var Tampon: string;
    Ligne, Key : string;
    xPos: integer;
begin

       Infos.URL := '';
       Infos.URLForModif := '';


     // dï¿½crypte
     Tampon := iaRSADecryptageKeyStream(TUNNEL_FORUM_DATA, RSA_GetPublicKey, CNT_PUBLICKEY_PASSPHRASE);

     Tampon := trim(sfcRemplace(Tampon, #$0D, ''));

    while Tampon <> '' do begin

        xPos := pos(#$0A, Tampon);
        if xPos = 0 then begin
           Ligne := Tampon;
           Tampon := '';
        end else begin
           Ligne := copy(Tampon, 1, xPos - 1);
           System.Delete(Tampon, 1, xPos);
        end;

        Ligne := trim(Ligne);
        Tampon := trimleft(Tampon);

        if Ligne = '' then Continue;
        if Ligne[1] = '#' then Continue;
        if Ligne[1] = '/' then Continue;
        if Ligne[1] = ';' then Continue;

        xPos := pos('=', Ligne);
        if xPos > 0 then begin
           Key := copy(Ligne, 1, xPos - 1);
           System.Delete(Ligne, 1, xPos);

           if SameText(Key, 'uo') then begin
              Infos.URL := Ligne;
           end else if SameText(Key, 'sb') then begin
              Infos.URLForModif := Ligne;
           end else if SameText(Key, 'dd') then begin
              Infos.URLDirOnly  := Ligne;
           end;

        end;
    end;

    if copy(Infos.URL, 1, 4) <> 'http' then begin
       Infos.URL := '';
       Infos.URLForModif := '';

    end else if copy(Infos.URLForModif, 1, 4) <> 'http' then begin
       Infos.URL := '';
       Infos.URLForModif := '';

    end else if length(Infos.URLDirOnly) < 5 then begin
       Infos.URL := '';
       Infos.URLForModif := '';

    end;

end;

constructor TCollectionTunnelSessionId.Create;
begin
    
    // Hï¿½ritage
    InHerited Create;
    
    
    
    // Code pour l'initialisation ici
    
    
end;

destructor TCollectionTunnelSessionId.Destroy;
begin
    // Implï¿½menter le code avant l'hï¿½ritage de la mï¿½thode
    // C'est ï¿½ dire ici
    
   
    // On appelle NOTRE clear qui lui-mï¿½me appelle NOTRE Delete qui libï¿½re l'espace mï¿½moire
    // avant d'enlever le pointeur de la TList
    // L'hï¿½ritage de Destroy appelle Clear, mais es-ce le notre ou celui de la classe de base ?
    Clear;
    
    
    // Hï¿½ritage
    InHerited Destroy;
end;


procedure TCollectionTunnelSessionId.Clear;
var i: integer;
begin

    if Count > 0 then begin
        For i := Count -1 downto 0 do
            Delete(i);
    end;
    
    inherited Clear;
end;


function TCollectionTunnelSessionId.Get_Pointeur(const Element: string): PTunnelSessionId;
var i: integer;
begin

   Result := nil;
   
   i := Get_Index(Element);
   if i < 0 then exit;
   Result := Items[i];

end;


function TCollectionTunnelSessionId.Get_Index(const Element: string): integer;
var i: integer;
    xEle, xTest: string;
    pElement: PTunnelSessionId;
begin

   Result := -1;

   if Count <= 0 then exit;
   xEle := lowercase(Element);

   for i := 0 to Count - 1 do begin

      pElement := Items[i];          {### ### ### ### ### ### ### ### ### ### ### ### ### ### }
      xTest := pElement^.sessionid;        {### Neccessite adpation en fonction du type record  ### }
      xTest := lowercase(xTest);     {### ### ### ### ### ### ### ### ### ### ### ### ### ### }


      if xEle = xTest then begin
         Result := i;
         exit;
      end;
   end;


end;

function TCollectionTunnelSessionId.Add_Update(Element: TTunnelSessionId): PTunnelSessionId;
var i : integer;
begin
     
     Result := nil;
                                         {### ### ### ### ### ### ### ### ### ### ### ### ### ### }
     i := Get_Index(Element.sessionid);        {### Neccessite adpation en fonction du type record  ### }
                                         {### ### ### ### ### ### ### ### ### ### ### ### ### ### }
     if i < 0 then begin 
        // L'ï¿½lï¿½ment n'existe pas, l'ajoute
        Result  := Add(Element);
     end else begin
        // L'ï¿½lï¿½ment existe dï¿½jï¿½, le met ï¿½ jour
        Result  := Items[i];     // Obtient le pointeur
        Result^ := Element;
     end;
     
end;




function TCollectionTunnelSessionId.Get_Item(Index: integer): TTunnelSessionId;
var pElement: PTunnelSessionId;
begin

     if Index < 0 then exit;
     if Index >= Count then exit;

     pElement := Items[Index];     // Obtient le pointeur
     Result := pElement^;

end;

function TCollectionTunnelSessionId.Delete(Index: integer): boolean;
var pElement: PTunnelSessionId;
begin

    Result := false;

    if (Index < 0) or (Index >= Count) then
       // Hors limite
       exit;


    // Obtient le pointeur
    pElement := Items[Index];

    if pElement <> nil then
       // Libï¿½re la mï¿½moire crï¿½ï¿½e lors de l'ajout
       Dispose(pElement);

    // Supprime le pointeur de la liste
    inherited Delete(Index);

    // Met le resultat ï¿½ True
    Result := true;

end;

function TCollectionTunnelSessionId.Delete(Element: string): boolean;
var i : integer;
begin

    Result := false;
    i      := Get_Index(Element);

    if i >= 0 then begin
       // Element trouvï¿½
       // Le supprime
       Delete(i);

       // Mï¿½me si la suppression Echoue on renvoi True
       // Car l'ï¿½lï¿½ment a ï¿½tï¿½ trouvï¿½
       Result := true;
    end;
end;


function TCollectionTunnelSessionId.Add(Element: TTunnelSessionId): PTunnelSessionId;
var pElement: PTunnelSessionId;
begin
    
    Result := nil;
    
    New(pElement);          // Crï¿½er l'espace mï¿½moire requis
    pElement^ := Element;   // Recopie les donnï¿½es dans l'emplacement pointï¿½
    
    try
       inherited Add(pElement);  // Ajoute le pointeur ï¿½ la liste
       Result := pElement;
    except
       Result := nil;
    end;
    
    // Pour libï¿½rer la mï¿½moire occupï¿½ par l'ï¿½lï¿½ment pointer:  Dispose(pElement);

end;

// Simple ajout de pointeur
procedure TCollectionTunnelSessionId.SimpleAdd(Pointeur: PTunnelSessionId);
begin
    
    inherited Add(Pointeur);  // Ajoute le pointeur ï¿½ la liste
    
end;

function TCollectionTunnelSessionId.GetItemx(Index: integer): TTunnelSessionId;
var pElement: PTunnelSessionId;
begin

   if Count < 0 then exit;
   if Index >= Count then exit;

   pElement := Items[Index];
   Result := pElement^;

end;



end.
